/*
 * @Author: yanjikun
 * @Date: 2022-03-21 14:43:56
 * @LastEditors: yanjikun
 * @LastEditTime: 2022-03-21 16:44:56
 * @Description: file content
 */

import { unref } from 'vue'
import { BasicProps } from '../props'
import { FormState } from './useFormState'

type useFormMethodsContext = FormState

export type FormMethods = ReturnType<typeof useFormMethods>

export const useFormMethods = (formMethodsContext: useFormMethodsContext) => {
  const { basicFormRef, formModel, getFormProps } = formMethodsContext
  /**
   * @description: 设置表单某个字段的值
   * @param {string} key 设置的key
   * @param {any} value 设置的值
   * @return {*}
   * @author: yanjikun
   */
  const setFormModel = (key: string, value: any) => {
    formModel[key] = value
    const { validateTrigger } = unref(getFormProps)
    if (!validateTrigger || validateTrigger === 'change') {
      basicFormRef.value?.validateFields([key])
    }
  }
  /**
   * @description: 添加表单属性
   * TODO 功能待完善
   * @param {BasicProps} formProps
   * @return {*}
   * @author: yanjikun
   */
  const setBasicFormProps = (formProps: BasicProps) => {
    console.log(formProps)
  }
  return {
    setFormModel,
    setBasicFormProps
  }
}
